﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.ComponentModel;
using SSATool.BL.DM.Properties;
using SSATool.Common.Util;
using SSATool.Common.Util.WeakEvents;
using SSATool.DL.Common;

#endregion

namespace SSATool.BL.DM
{
    /// <summary>
    /// Represents a service instance.
    /// </summary>
    public class Service : DisposeBase, INotifyPropertyChanged
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly FastSmartWeakEvent<PropertyChangedEventHandler> _onPropertyChanged = new FastSmartWeakEvent<PropertyChangedEventHandler>();

        private readonly ServiceCollection _parent;
        private bool _canPauseAndContinue;
        private bool _canShutdown;
        private bool _canStop;
        private string _displayName;
        private string _machineName;
        private string _serviceName;
        private ServiceStatus _status = ServiceStatus.Unknown;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a value indicating whether the service can be paused and resumed.
        /// </summary>
        /// <returns>
        /// Returns true if the service can be paused; otherwise, false.
        /// </returns>
        public bool CanPauseAndContinue
        {
            get { return _canPauseAndContinue; }
            internal set
            {
                if (value != _canPauseAndContinue)
                {
                    _canPauseAndContinue = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("CanPauseAndContinue"));
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether the service should be notified when the system
        /// is shutting down.
        /// </summary>
        /// <returns>
        /// Returns true if the service should be notified when the system is shutting down;
        /// otherwise, false.
        /// </returns>
        public bool CanShutdown
        {
            get { return _canShutdown; }
            internal set
            {
                if (value != _canShutdown)
                {
                    _canShutdown = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("CanShutdown"));
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether the service can be stopped after it has started.
        /// </summary>
        /// <returns>
        /// Returns true if the service can be stopped; otherwise, false.
        /// </returns>
        public bool CanStop
        {
            get { return _canStop; }
            internal set
            {
                if (value != _canStop)
                {
                    _canStop = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("CanStop"));
                }
            }
        }

        /// <summary>
        /// Gets or sets a friendly name for the service.
        /// </summary>
        public string DisplayName
        {
            get { return _displayName; }
            internal set
            {
                if (value != _displayName)
                {
                    _displayName = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
                }
            }
        }

        /// <summary>
        /// Gets or sets the name of the computer on which this service resides.
        /// </summary>
        public string MachineName
        {
            get { return _machineName; }
            internal set
            {
                if (value != _machineName)
                {
                    _machineName = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("MachineName"));
                }
            }
        }

        /// <summary>
        /// Gets or sets the name that uniquely identifies the service that this instance references.
        /// </summary>
        public string ServiceName
        {
            get { return _serviceName; }
            internal set
            {
                if (value != _serviceName)
                {
                    _serviceName = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("ServiceName"));
                }
            }
        }

        /// <summary>
        /// Gets/sets the status of the service that is referenced by this instance.
        /// </summary>
        public ServiceStatus Status
        {
            get { return _status; }
            internal set
            {
                if (value != _status)
                {
                    _status = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Status"));
                }
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="Service"/> class.
        /// </summary>
        internal Service(ServiceCollection parent)
        {
            PreCondition.AssertNotNull(parent, "parent");
            _parent = parent;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Starts the current service.
        /// </summary>
        public void Start()
        {
            Status = ServiceStatus.StartPending;
            _parent.Context.DataProvider.SetService(_parent.Converter.CreateServiceController(this));
        }

        /// <summary>
        /// Stops the current service.
        /// </summary>
        public void Stop()
        {
            if (!CanStop)
                throw new InvalidOperationException(Resources.ExcServiceCouldNotBeStopped);

            Status = ServiceStatus.StopPending;
            _parent.Context.DataProvider.SetService(_parent.Converter.CreateServiceController(this));            
        }

        /// <summary>
        /// Pauses the current service.
        /// </summary>
        public void Pause()
        {
            if (!CanPauseAndContinue)
                throw new InvalidOperationException(Resources.ExcServiceCouldNotBePausedAndContinued);

            Status = ServiceStatus.PausePending;
            _parent.Context.DataProvider.SetService(_parent.Converter.CreateServiceController(this));                        
        }

        /// <summary>
        /// Continues the current service.
        /// </summary>
        public void Continue()
        {
            if (!CanPauseAndContinue)
                throw new InvalidOperationException(Resources.ExcServiceCouldNotBePausedAndContinued);

            Status = ServiceStatus.ContinuePending;
            _parent.Context.DataProvider.SetService(_parent.Converter.CreateServiceController(this));                        
        }

        /// <summary>
        /// Copies the values of the given service into the current instance.
        /// </summary>
        /// <param name="other">Specifies the other (source) service.</param>
        internal void CopyValuesFrom(Service other)
        {
            PreCondition.AssertNotNull(other, "other");

            CanPauseAndContinue = other.CanPauseAndContinue;
            CanShutdown = other.CanShutdown;
            CanStop = other.CanStop;
            DisplayName = other.DisplayName;
            MachineName = other.MachineName;
            ServiceName = other.ServiceName;
            Status = other.Status;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { _onPropertyChanged.Add(value); }
            remove { _onPropertyChanged.Remove(value); }
        }

        /// <summary>
        /// Fires the PropertyChanged event.
        /// </summary>
        /// <param name="args">Event args to pass to the receivers.</param>
        protected void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            _onPropertyChanged.Raise(this, args);
        }

        #endregion
    }
}
